package com.dlut.connection;

import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionTest {

    //方式一
    @Test
    public void testConnection1() throws SQLException {
        Driver driver = new com.mysql.jdbc.Driver();
        String url = "jdbc:mysql://localhost:13306/test";
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","123456");

        Connection conn = driver.connect(url, info);

        System.out.println(conn);

    }

    //方式二, 通过反射，使程序中不出现第三方的api，具有更好的移植性
    @Test
    public void testConnection2() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, SQLException {
        //1. 使用反射获取Driver类实现对象
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
//        Driver driver = (Driver) clazz.newInstance();
        Driver driver = (Driver) clazz.getDeclaredConstructor().newInstance();

        //2. 要链接的数据库
        String url = "jdbc:mysql://localhost:13306/test";

        //3.用户名，密码
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","123456");

        //4. 获取链接
        Connection conn = driver.connect(url, info);

        System.out.println(conn);
    }

    //方式三 使用DriverManager替换Driver
    @Test
    public void testConnection3() throws Exception{
        //1. 获取Driver类实现对象
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.getDeclaredConstructor().newInstance();

        //2. 提供三个链接的基本信息
        String url = "jdbc:mysql://localhost:13306/test";
        String user = "root";
        String password = "123456";

        //3. 注册驱动
        DriverManager.registerDriver(driver);

        //4. 获取链接
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }

    //方式四 使用DriverManager替换Driver， 省略第三步
    @Test
    public void testConnection4() throws Exception{
        //1. 提供三个链接的基本信息
        String url = "jdbc:mysql://localhost:13306/test";
        String user = "root";
        String password = "123456";

        //2. 获取Driver类实现对象（可以省略，但是没有必要）
        Class.forName("com.mysql.jdbc.Driver");

        //省略下面的操作，Driver的静态代码块中注册过了
//        Driver driver = (Driver) clazz.getDeclaredConstructor().newInstance();
//        DriverManager.registerDriver(driver);

        //4. 获取链接
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }


    /**
     * 方式五(最终版) 将数据库需要的基本信息写在配置文件中
     * 好处？
     * 1. 实现了数据和代码的分离
     * 2. 如果需要修改配置文件信息，可以避免对程序重新打包
     *
     * @throws Exception
     */
    @Test
    public void testConnection5() throws Exception{
        //1. 读取配置文件中的4个基本信息
        InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");

        Properties pros = new Properties();
        pros.load(is);

        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");

        //2. 加载驱动
        Class.forName(driverClass);

        //3. 获取链接
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }
}
